<?php
namespace app\index\controller;

use app\admin\model\UserModel;
use think\Controller;
use think\Db;
use app\index\model\ConfigModel;
use app\index\model\CardModel;
use app\index\model\LoanModel;
use app\index\model\ApplyModel;
use app\index\model\UsersModel;
use app\index\model\CashModel;
use app\index\model\UserMoney;
use think\Request;
class User extends Controller{
    public function index(){
        $usersModel=new UsersModel();
        $this->assign([
            'user'=>$usersModel->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }
    public function myself(){
        $param=input('param.');
        $model = new UsersModel();
        $this->assign([
            'user'=>$model->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }
    public function profile(){
        $param=input('param.');
        $model = new UsersModel();
        if(request()->isPost()) {
            return json(msg(1, $model->profile($param), ''));
        }
        $this->assign([
            'user'=>$model->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }
    // 上传缩略图
    public function uploadImg(){
        if(request()->isAjax()){
            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'upload/user');
            if($info){
                $src =  '/upload/user' . '/' . date('Ymd') . '/' . $info->getFilename();
                Db::name('users')->where(array('user_id'=>session('user_id')))->update(array('wxqr'=>$src));
                return json(msg(1, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }
    public function bindMobile(){
        $param=input('param.');
        if(request()->isPost()) {
            $model = new UsersModel();
            return json(msg(1, $model->bindMobile($param), ''));
        }
        return $this->fetch();
    }
    public function myVip(){
        $param=input('param.');
        $model = new UsersModel();
        $this->assign([
            'user'=>$model->getMyVip(session('user_id')),
        ]);
        return $this->fetch();
    }
    public function myQrcode(){
        $param=input('param.');
        $model = new UsersModel();
        $this->assign([
            'user'=>$model->getMyVip(session('user_id')),
        ]);
        return $this->fetch();
    }
    public function team(){
        $usersModel=new UsersModel();
        $upuser=$usersModel->findUpuserById(session('user_id'));
        $hasUpuser=0;
        if(!empty($upuser)) $hasUpuser=1;
        $this->assign([
            'hasUpuser'=>$hasUpuser,
            'upuser'=>$upuser,
            'statement'=>Db::name('config')->where(array('inc_type'=>'team_income_desc'))->value('value')
        ]);
        return $this->fetch();
    }

    public function userMoney(){
        $param=input('param.');
        if(request()->isPost()) {
            $model = new UserMoney();
            return json(msg(1, $model->getLists($param), ''));
        }
        return $this->fetch();
        /*
        $this->assign([
            'data'=>$model->getLists($param)
        ]);
        return $this->fetch();
        */
    }

    public function statistics(){
        $param=input('param.');
        $model=new UsersModel();
        $this->assign([
            'data'=>$model->statistics()
        ]);
        return $this->fetch();
    }

    public function drive(){
        $param=input('param.');
        if(request()->isPost()) {
            $model = new ApplyModel();
            return json(msg(1, $model->getDrive($param), ''));
        }
        return $this->fetch();
    }

    public function poster(){
        $param=input('param.');
        $url=request()->domain();
        $recode=base64_encode(session('user_id'));
        $url=$url.'?recode='.$recode;
        $this->assign([
            //'user'=>$usersModel->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }

    public function invite(){
        $param=input('param.');
        $url=request()->domain();
        $recode=base64_encode(session('user_id'));
        $url=$url.'?recode='.$recode;
        $model=new UsersModel();
        $this->assign([
            'user'=>$model->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }

    public function msg(){
        $param=input('param.');
        $model=new UsersModel();
        $this->assign([
            'data'=>$model->myMsg($param)
        ]);
        return $this->fetch();
    }

    public function service(){
        $param=input('param.');
        $model=new ConfigModel();
        $this->assign([
            'data'=>$model->getService()
        ]);
        return $this->fetch();
    }
    public function advice(){
        $param=input('param.');
        if(request()->isPost()) {
            $check=paramCheck($param,'content,contact');
            if($check['code']!=1) return json($check);
            $model = new ConfigModel();
            return json($model->advice($param));
        }
        return $this->fetch();
    }

    public function withdraw(){
        $usersModel=new UsersModel();
        $this->assign([
            'user'=>$usersModel->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }
    public function alipay(){
        $usersModel=new UsersModel();
        $this->assign([
            'user'=>$usersModel->findUserById(session('user_id')),
        ]);
        return $this->fetch();
    }
    public function cash(){
        $param=input('param.');
        $check=paramCheck($param,'mobile,money,bank_name,bank_card,realname');
        if($check['code']!=1) return json($check);
        $model=new CashModel();
        return json($model->add($param));
    }

    public function apply(){
        $param=input('param.');
        if(request()->isPost()){
            $check=paramCheck($param,'pid,type,realname,card_num,mobile,code,session_id');
            if($check['code']!=1) return json($check);
            $model=new ApplyModel();
            return json($model->add($param));
        }
        $check=paramCheck($param,'pid,type');
        if($check['code']!=1) return json($check);
        $this->assign([
            'data' => $param
        ]);
        return $this->fetch();
    }
}
